package com.hex.ds.hdrs.calendar.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.hex.bigdata.hbda.base.Page;
import com.hex.ds.hdrs.calendar.dao.WorkCalendarDao;
import com.hex.ds.hdrs.calendar.dto.WorkCalendarDto;
import com.hex.ds.hdrs.calendar.po.WorkCalendar;
import com.hex.ds.hdrs.common.utils.BaseUtil;
import com.hex.ds.hdrs.common.utils.MapUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class WorkCalendarService {

    @Autowired
    private WorkCalendarDao workCalendarDao;

    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 分页查询
     *
     * @param workCalendar
     * @param page
     * @return
     */
    public List<WorkCalendar> selectPages(WorkCalendar workCalendar, Page page) {
        log.debug("【进入了WorkCalendarService的selectPages】的workCalendar: {}" , workCalendar);
        Map map = MapUtil.convertObjToMap(workCalendar);
        return this.workCalendarDao.selectAll(map, page);
    }

    /**
     * @Method: del
     * @Param: [pkIds]
     * @Return: void
     * @Description: 删除工作日历
     * @Author: ji.tang
     * @Date: 2022/11/22 14:54
     * @Version: V1.0
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void del(List<String> pkIds) {
        log.debug("【进入了WorkCalendarService的del】pkIds:{}" ,pkIds);
        for (String pkId : pkIds) {
            this.workCalendarDao.del(pkId);
        }
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void add(WorkCalendarDto workCalendarDto) {
        log.debug("【进入了WorkCalendarService的add】workCalendarDto：{}" , workCalendarDto);
        if (StrUtil.isNotBlank(workCalendarDto.getDateType()) && StrUtil.isNotBlank(workCalendarDto.getDateTime())){
            WorkCalendar workCalendar = new WorkCalendar();
            workCalendar.setPkId(BaseUtil.getPkId());
            workCalendar.setDateTime(workCalendarDto.getDateTime());
            workCalendar.setDateType(workCalendarDto.getDateType());
            workCalendar.setDateDes(workCalendarDto.getDateDes());
            this.workCalendarDao.insert(workCalendar);
        }
    }

    /*
     * @Method: addDefaultCalendar <br>
     * @Param: [checkList] <br>
     * @Return: void <br>
     * @Description：添加默认工作时间<br>
     * @Author： wz.li<br>
     * @Date： 2022/12/2 17:03 <br>
     * @Version： V2.0.2<br>
     */
    public void addDefaultCalendar(List<String> checkList){
        if (CollectionUtil.isNotEmpty(sysConfigService.getSysConfigValue())){
            sysConfigService.edit(checkList);
        }else {
            sysConfigService.add(checkList);
        }
    }

   /*
    * @Method: getDefaultWorkCalendar <br>
    * @Param: [] <br>
    * @Return: java.util.List<java.lang.String> <br>
    * @Description：获取默认工作日<br>
    * @Author： wz.li<br>
    * @Date： 2022/12/2 17:26 <br>
    * @Version： V2.0.2<br>
    */
    public List<String> getDefaultWorkCalendar(){
        return sysConfigService.getSysConfigValue();
    }

    public WorkCalendarDto goEdit(String dateTime) {
        WorkCalendar workCalendar = this.workCalendarDao.getWorkCalendar(dateTime);
        WorkCalendarDto workCalendarDto = new WorkCalendarDto();
        if (workCalendar != null) {
            workCalendarDto.setPkId(workCalendar.getPkId());
            workCalendarDto.setDateTime(workCalendar.getDateTime());
            workCalendarDto.setDateType(workCalendar.getDateType());
            workCalendarDto.setDateDes(workCalendar.getDateDes());
        }
        return workCalendarDto;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void edit(WorkCalendarDto workCalendarDto) {
        log.debug("【进入了WorkCalendarService的edit】workCalendarDto：{}" , workCalendarDto);
        WorkCalendar workCalendar = new WorkCalendar();
        workCalendar.setPkId(workCalendarDto.getPkId());
        workCalendar.setDateTime(workCalendarDto.getDateTime());
        workCalendar.setDateType(workCalendarDto.getDateType());
        workCalendar.setDateDes(workCalendarDto.getDateDes());
        this.workCalendarDao.edit(workCalendar);
    }

    public Boolean checkData(WorkCalendar workCalendar) {
        log.info("【进入了WorkCalendarService的checkAppData】参数workCalendar: {}" , workCalendar.toString());
        WorkCalendar dbworkCalendar = this.workCalendarDao.checkData(workCalendar.getDateTime());
        if (dbworkCalendar != null && !dbworkCalendar.getPkId().equals(workCalendar.getPkId())) {
            return false;
        }
        return true;
    }

    /**
     * 判断是否是工作日
     */
    public Boolean checkDateStyle(String bizDate){
        //1.判断日历中是否有该日期
        if (this.workCalendarDao.checkDateStyle(bizDate)){
            return true;
        }else{
            //2.判断是否在默认配置工作日中
            List<String> weeks = this.sysConfigService.getSysConfigValue();
            // 1表示周日，2-6表示周一到周五，7表示周六，项目中配置1-7表示周一到周日
            int week = DateUtil.dayOfWeek(DateUtil.parse(bizDate,"yyyyMMdd"));
            return CollectionUtil.isNotEmpty(weeks) && weeks.contains(mapDayOfWeek(week));
        }
    }

    public String mapDayOfWeek(int actualWeek) {
        // 如果是周日（1），映射为7
        if (actualWeek == 1) {
            return "7";
        } else {
            // 否则，将星期减一映射到项目中的星期（将 2-7 转换为 1-6）
            return StrUtil.toString(actualWeek - 1);
        }
    }

    /**
     * @Method checkWorkCalendar <br>
     * @Param
     * @Param bizDate <br>
     * @Return boolean <br>
     * @Description 判断自定义日历设置<br>
     * @Author jian.qiao<br>
     * @Date 2023/11/27 14:54 <br>
     * @Version V1.0<br>
     */
    public boolean checkWorkCalendar(String bizDate,boolean isWorkDay){
        Date date = DateUtil.parse(bizDate, "yyyyMMdd");
        String formattedDate = DateUtil.format(date, "yyyy-MM-dd");
        WorkCalendar workCalendar = this.workCalendarDao.getWorkCalendar(formattedDate);
        if (workCalendar == null){
            //未自定义日历，返回默认工作日判断
            return isWorkDay;
        }
        //0 休息日 1 工作日
        return StrUtil.equalsAnyIgnoreCase(workCalendar.getDateType(),"0") ? false : true;
    }
}
